package edu.cmu.cs.webapp.ioms.ui.wizards.product;

import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

import edu.cmu.cs.webapp.ioms.models.Product;

public class ProductMainWizardPage extends WizardPage {

	// private Text _orderIDTxt;
	// private DateTime _orderDateTxt;
	// private Combo _statusTxt;
	// private DateTime _shippingDate;
	// private Text _shippingCity;
	// private Combo _shippingState;
	// private Text _shippingCountry;

	private Text _productName;
	// need to decide later whether to include another pop-up box for this
	// functionality
	private Text _price;
	private Text _description;

	protected ProductMainWizardPage(String pageName) {
		super(pageName);
		setTitle("Product Main");
		setDescription("Please input the information for a new product");
	}

	public ProductMainWizardPage(String pageName, String title,
			ImageDescriptor titleImage) {
		super(pageName, title, titleImage);
	}

	@Override
	public void createControl(Composite parent) {
		// direct container under parent
		Composite container = new Composite(parent, SWT.NULL);
		GridLayout layout = new GridLayout();
		container.setLayout(layout);
		// for general item
		GridData gd = new GridData(GridData.BEGINNING);
		gd.minimumWidth = 50;
		// for description lable
		GridData gd1 = new GridData(GridData.HORIZONTAL_ALIGN_CENTER
				| GridData.FILL_HORIZONTAL);
		gd1.horizontalSpan = 2;

		// for description dialog
		GridData gd2 = new GridData(GridData.HORIZONTAL_ALIGN_CENTER
				| GridData.FILL_BOTH);
		gd2.horizontalSpan = 2;
		gd2.verticalSpan = 5;
		gd2.grabExcessVerticalSpace = true;

		GridData gd3 = new GridData(GridData.FILL_HORIZONTAL);

		layout.numColumns = 2;
		// layout.verticalSpacing = 9;

		// set the first column in the contianer, usually is the name of the
		// input
		Label label = new Label(container, SWT.NULL);
		label.setText("Product Name*:"); //$NON-NLS-1$
		_productName = new Text(container, SWT.BORDER | SWT.SINGLE);
		_productName.setLayoutData(gd3);
		_productName.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				validate();
			}
		});
		this._productName.setEditable(true);

		label = new Label(container, SWT.NULL);
		label.setText("Product price($):");
		label.setLayoutData(gd);
		_price = new Text(container, SWT.BORDER | SWT.SINGLE);
		_price.setLayoutData(gd3);
		_price.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				validate();
			}
		});
		this._price.setEditable(true);
		
		// create a line between the product name and other element, deliminator
		createLine(container, layout.numColumns);

		label = new Label(container, SWT.NULL);
		label.setLayoutData(gd1);
		label.setText("Product Description:"); //$NON-NLS-1$

		_description = new Text(container, SWT.BORDER | SWT.NO_SCROLL
				| SWT.MULTI);
		_description.setLayoutData(gd2);

		_description.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				validate();
			}
		});

		/*
		 * _description = new Text(container, SWT.BORDER | SWT.SINGLE);
		 * _description.setLayoutData(gd); _description.addModifyListener(new
		 * ModifyListener() { public void modifyText(ModifyEvent e) {
		 * validate(); } });
		 */

		if (isUpdate()) {
			loadFromExistingModel();
		}

		setControl(container);
		validate();
		container.pack(true);
	}

	private void loadFromExistingModel() {
		// put the selected information into the pop-ups
		Product model = getModel();

		// if current model has been chosen and is not null;
		if (model != null) {
			_productName.setText(model.getProd_name() == null ? "" : model
					.getProd_name());
			_price.setText("" + model.getPrice());
			_description.setText(model.getProd_description() == null ? ""
					: model.getProd_description());

		}
	}

	private Product getModel() {
		return ((ProductWizard) getWizard()).getModel();
	}

	private void createLine(Composite container, int numColumns) {
		Label line = new Label(container, SWT.SEPARATOR | SWT.HORIZONTAL
				| SWT.BOLD);
		GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
		gridData.horizontalSpan = numColumns;
		line.setLayoutData(gridData);

	}

	private boolean validate() {
		
		boolean valid = true;
		StringBuffer errorMessage = new StringBuffer();

		if (_productName == null || _productName.getText().length() == 0) {
			errorMessage.append("You must enter a product name");
			valid= false;
		}
		try {
			Double price = Double.parseDouble(_price.getText());
		} catch (NumberFormatException ex) {
			errorMessage.append("Please input valid number for price");
			valid =false;
		}
		if (valid){
			setErrorMessage(null);
			setPageComplete(true);
		}
		else {
			setErrorMessage(errorMessage.toString());
			setPageComplete(false);
		}
		
		return valid;
	}

	private boolean isUpdate() {
		return ((ProductWizard) getWizard()).isUpdate();
	}

	public String get_productName() {
		return _productName.getText();
	}

	public double get_price() {
		return Double.parseDouble(_price.getText().trim());
	}

	public String get_description() {
		return _description.getText();
	}

}
